/**
 * 
 */
package com.thon.controller.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.thon.commons.security.utils.ShiroUser;
import com.thon.commons.utils.PropertiesLoader;

/**
 * @author THON
 * @email thon.ju@meet-future.com
 * @date 2011-11-23 下午03:20:23
 * @description:
 */
public abstract class BaseController {
	private static final Log log = LogFactory.getLog(BaseController.class);
	private static final String REGEX_EMAIL = "^[A-Za-z0-9][\\w\\-\\.]{1,12}@([\\w\\-]+\\.)+[\\w]{2,3}$";
	private static final String REGEX_PHONE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
	private static final String REGEX_NICKNAME = "^[\u4E00-\u9FA5a-zA-Z0-9]{2,10}$";
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));
	}
	
	@ModelAttribute
	public Model initVersion(HttpSession session, Model model) {
		model.addAttribute("version", PropertiesLoader.getConfiguration("application.properties", "version"));
		return model;
	}
	
	/**
	 * 返回站点地址
	 * @param request
	 * @return
	 */
	public String getBasePath(HttpServletRequest request){
		return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath();
	}
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	protected Integer getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
	
	public String getResource(String  key){
		try {
			return ResourceBundle.getBundle("config").getString(key);
		} catch (Exception e) {
			return "Can't find resource for bundle";
		}
	}
	
	public String trimString(String rawString) {
		return rawString.replaceAll("\"", "");
	}
	
	// 邮箱格式
	public Boolean checkEmailPattern(String email){
	    Pattern   pattern   =   Pattern.compile(REGEX_EMAIL);  
	    Matcher   matcher   =   pattern.matcher(email);  
	    return matcher.find();
	}
	
	// 电话格式
	public Boolean checkPhonePattern(String phone){
	    Pattern   pattern   =   Pattern.compile(REGEX_PHONE);  
	    Matcher   matcher   =   pattern.matcher(phone);  
	    return matcher.find();
	}
	
	// 昵称格式
	public Boolean checkNicknamePattern(String nickname){
	    Pattern   pattern   =   Pattern.compile(REGEX_NICKNAME);  
	    Matcher   matcher   =   pattern.matcher(nickname);  
	    return matcher.find();
	}
	
}
